﻿(function ($) {
    //获取url参数支持中文
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]); return null;
    }
})(jQuery);

(function ($) {
    $.fn.extend({
        //初始化Tree
        getSelectNode: function () {
            if (this[0].selectNode == undefined) {
                return { id: 0 };
            }
            return this[0].selectNode;
        },
        initTree: function (url, changed, data) {

            var selectNode = undefined;
            this.jstree({
                'core': {
                    "multiple": false,
                    'data': {
                        "dataType": 'json',
                        "type": "post",
                        "url": url,
                        "data": function (node) {
                            if (data != undefined) {
                                if (node.id != "#") {
                                    data.id = node.id
                                }
                                return data;
                            }
                            return { "id": node.id };
                        }
                    },
                    'dblclick_toggle': true          //tree的双击展开
                }
            });
            this.on('loaded.jstree', function (e, data) {
                var inst = data.instance;
                if (this.selectNode == undefined) {
                    this.selectNode = inst.get_node(e.target.firstChild.firstChild.lastChild);
                }
                inst.select_node(this.selectNode);
            });
            this.on("changed.jstree", function (e, data) {
                if (data.node == undefined) {
                    return;
                }
                this.selectNode = data.node;
                if ($.isFunction(changed)) {
                    changed(e, data);
                }
            });
            return this;
        }
    })
})(jQuery);


(function ($) {
    $.fn.extend({
        //初始化Table
        initTable: function (url, columns, height, queryParams, onLoadSuccess, pagination) {
            pagination = pagination == undefined ? true : pagination 
            var $table = this;
            function getHeight() {
                if ((height + "").indexOf("px") > 0) {
                    return height.replace("px", "");
                } else if (isNaN(parseInt(height))) {
                    return 'auto';
                }
                return $(window).height() - height;
            }
            if (typeof (url) != "string") {
                $table.bootstrapTable({
                    data: url, 
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: pagination,                   //是否启用分页 
                    pageNumber: 1,                      //初始化加载第一页，默认第一页
                    pageSize: 20,                       //每页的记录行数（*）
                    pageList: [10, 20, 30, 40, 50],     //可供选择的每页的行数（*）
                    clickToSelect: true,                //是否启用点击选中行
                    height: getHeight(),
                    columns: columns,
                    onLoadSuccess: function (data) {
                        if ($.isFunction(onLoadSuccess)) {
                            onLoadSuccess(data);
                        }
                    }
                });
            } else {
                $table.bootstrapTable({
                    url: url,            //数据来源地址
                    method: 'post',                     //数据请求方式
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: pagination,                   //是否启用分页
                    sidePagination: 'server',           //在服务器端分页
                    queryParams: queryParams,           //传递参数
                    pageNumber: 1,                      //初始化加载第一页，默认第一页
                    pageSize: 20,                       //每页的记录行数（*）
                    pageList: [10, 20, 30, 40, 50],     //可供选择的每页的行数（*）
                    clickToSelect: true,                //是否启用点击选中行
                    height: getHeight(),
                    columns: columns,
                    onLoadSuccess: function (data) {
                        if ($.isFunction(onLoadSuccess)) {
                            onLoadSuccess(data);
                        }
                    }
                });
            }

            $(window).resize(function () {
                $table.bootstrapTable('resetView', {
                    height: getHeight()
                });
            });
            setTimeout(function () {
                $table.bootstrapTable('resetView');
            }, 200);
            return $table;
        }
    })
})(jQuery);



(function ($) {
    $.fn.extend({
        //from 转Json对象
        formToJson: function (excludeEmpty) {
            var from = this;
            var data = {};
            var array = from.serializeArray();
        
            for (var i = 0; i < array.length; i++) { 
                if (typeof (excludeEmpty) === "boolean" && excludeEmpty && (array[i].value == "" || array[i].value == undefined || array[i].value == null)) {
                    continue;
                }
                data[array[i].name] = array[i].value;
            }
            return data;
        }
    })
})(jQuery);
(function ($) {
    $.fn.extend({
        //Json对象转from 
        JsonToform: function (json) {
            var input = $(this).find('input,select');
            for (var i = 0; i < input.length; i++) {
                var temp = $(input[i]).attr("id");
                if (temp == undefined) continue;
                var type = input[i].type;
                if (type.indexOf("select-") > -1) type = type.split('-')[0];
                if (type == "checkbox") {
                    temp = $(input[i]).parent().next().attr("id"); 
                }
                for (var key in json) {
                    if (temp.toLowerCase() == key.toLocaleLowerCase()) {
                        if (type == "checkbox") {
                            var cbclass = $(input[i]).parent().attr("class");
                            if (cbclass != undefined && cbclass.indexOf("icheckbox") > -1) {
                                $(input[i]).iCheck('check');
                                $(input[i]).trigger("ifChecked");
                            } else {
                                $(input[i]).attr("checked", true);
                            }

                        } else {
                            $(input[i]).val(json[key]);
                            $(input[i]).trigger("change");
                        }

                        continue;
                    }
                }
            }
           
        }
    })
})(jQuery);
